<?php
include '../Module/AlbumModule.php';
$alModule=new AlbumModule();

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$Module=new ProductModule();
if(isset($_REQUEST["id"])){
    $id= $_REQUEST["id"];
    $item=$Module->getProduct($id);
    genErorMSG($Module->geterror());
}
?>
    
<!--    <div class="mybutton icon tooltip" title="Back" onclick="hideproductdetail();"><div><img src="./images/icon/left1.png"/><span>Back</span></div></div>
    <div class="mybutton icon spaceright tooltip" title="Reload" onclick="<?=isset($id)?"loadproductdetail('$id')":"loadproductdetail(null);"?>"><div><img src="./images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>-->
    

    <div style="clear: both"></div>
    <div style="height: 40px;width: 100%;text-align: center">
        <h1><?=isset($id)?"Chỉnh Sửa Sản Phẩm : ":"Thêm Sản Phẩm Mới"?><?=$item["name"]?></h1>
    </div>
    <div style="width: 700px;margin: 0 auto">
        <table style="width:100%;" class="_form">
                    <tr>
                        <td style="">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtpro_name" placeholder="" value="<?=$item["name"]?>"/>
                                    <input type="hidden" id="txtpro_id" placeholder="" value="<?=$item["id"]?>"/>
                                    <div class="label">
                                        <label>Tên</label>
                                        <div class="warning-16 tooltip hover50" title="Tên sản Phẩm không được để trống"></div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtpro_title" placeholder="" value="<?=$item["title"]?>"/>
                                    <div class="label">
                                        <label>Tiêu Đề</label>
                                        <div class="warning-16 tooltip hover50" title="Tiêu đề của sản phẩm không được để trống"></div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtpro_price" placeholder="" value="<?=$item["price"]?>"/>
                                    <div class="label">
                                        <label>Giá Bán</label>
                                        <div class="warning-16 tooltip hover50" title="Giá bán của sản phẩm không được để trống"></div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr class="hidden">
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtpro_smallimage" placeholder="" value="<?=$item["smallimage"]?>"/>
                                    <div class="label">
                                        <label>Ảnh Nhỏ</label>
                                        <div class="warning-16 tooltip hover50" title="Hình ảnh thu nhỏ của sản phẩm.Có thể bỏ trống."></div>
                                    </div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtpro_smallimage' );"
                                title="Chọn Hình Ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtpro_image" placeholder="" value="<?=$item["image"]?>"/>
                                    <div class="label">
                                        <label>Hình Ảnh</label>
                                        <div class="warning-16 tooltip hover50" title="Hình ảnh gốc của sản phẩm không được để trống"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtpro_image' );"
                                title="Chọn Hình Ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label" type="select">
                            <div class="input" type="select">
                                <div class="select">
                                    <select id="cbxpro_albumidi">
                                        <option value="0">--- Chọn Album ---</option>
                                        <?
                                        $albumid=$item["album"];
                                        $albums=$alModule->getAlbums();
                                        foreach ($albums as $row){
                                            if($row["id"]==$albumid)
                                                echo "<option selected=1 value='".$row["id"]."'>".$row["album"]."</option>";
                                            else
                                                echo "<option value='".$row["id"]."'>".$row["album"]."</option>";


                                        }
                                        unset($row);unset($albums);unset($alModule);
                                        ?>
                                    </select>
                                </div>
                                <div class="label"><label>Album</label></div>
                            </div>
                            </div>
                        </td>
                    </tr>
                    
                    
                    
                    
                    <tr style="display: none">
                        <td>
                            <div class="input-full">
                                <div class="label"><label>Mô tả</label></div>
                            </div>
                            <div style="padding:27px 0px 0 0;" id="editor_content">

                            </div>
                                <textarea class="_s _w100 mceEditor" id="txtpro_des" rows="3" cols="103" 
                                            style="width: 100%;height: 200px"

                                            ><?=$item["description"]?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="">
                            <span style="color: blue">Thêm Lúc : <?=$item["insert"]?></span>
                            <?
                            $u=$item["update"];$d=$item["delete"];
                            if(isset($item["update"])){
                                echo "<br/><span style='color: orange'>Cập Nhật lúc : $u</span>";
                            }
                            if(isset($item["delete"])){
                                echo "<br/><span style='color: red'>Xóa lúc : $d</span>";
                            }
                            ?>

                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="mybutton icon tooltip<?=isset($item["delete"])?" disable":""?>" 
                            title="Save"
                            onclick="<?=isset($item["delete"])?"":(isset($id)?"update();":"save();")?>"
                            ><div><img src="./images/icon/media_floppy_green.png"/><span>Save</span></div></div>
                        
                        </td>
                    </tr>
                </table>


        <span id="pro_warningmsg"></span>
    </div>
    <div style="clear: both"></div>

<!--    <div class="mybutton icon tooltip" title="Back" onclick="hideproductdetail();"><div><img src="./images/icon/left1.png"/><span>Back</span></div></div>
    <div class="mybutton icon spaceright tooltip" title="Reload" onclick="<?=isset($id)?"loadproductdetail('$id')":"loadproductdetail(null);"?>"><div><img src="./images/icon/arrow_refresh1.png"/><span>Reload</span></div></div>-->
    
<!--    <div class="mybutton icon tooltip<?=isset($item["delete"])?" disable":""?>" 
         title="Save"
         onclick="<?=isset($item["delete"])?"":(isset($id)?"update();":"save();")?>"
         ><div><img src="./images/icon/media_floppy_green.png"/><span>Save</span></div></div>
    <div class="mybutton icon tooltip<?=isset($id)?"":" disable"?>" 
         onclick="<?=isset($id)?"loadproductdetail(null);":""?>"
         title="Add New Product"
         ><div><img src="./images/icon/addg.png"/><span>Add</span></div></div>

    <div style="clear: both"></div>-->
    
    <script>
        $(document).ready(function(){
            $(".tooltip").tipTip({defaultPosition:"top", edgeOffset: 10});
            tinyMCE.init({
                    mode : "textareas",
                    theme : "advanced",
                    content_css : "/Coffee/css/style.css",
                    editor_selector : "mceEditor",
                    editor_deselector : "mceNoEditor",
                    theme_advanced_resizing : true,
                    theme_advanced_resize_horizontal : false
            });
        });
        
        function save(){
            process("add");
        }
        function update(){
            process("edit");
        }
        function process(action){
            
            if(!_FcheckFilled($("#txtpro_name").val())){$("#txtpro_name").focus();return;}
            if(!_FcheckFilled($("#txtpro_title").val())){$("#txtpro_title").focus();return;}
            if(!_FcheckFilled($("#txtpro_price").val())){$("#txtpro_price").focus();return;}
            if(!_FcheckNumber($("#txtpro_price").val())){$("#txtpro_price").focus();return;}
            if(!_FcheckFilled($("#txtpro_image").val())){$("#txtpro_image").focus();return;}
            var surl="./Controller/";
            
            var sdata={
                    "controller":"ProductController",
                    "action":action,
                    "id":$("#txtpro_id").val(),
                    "name":$("#txtpro_name").val(),
                    "title":$("#txtpro_title").val(),
                    "smallimage":$("#txtpro_smallimage").val(),
                    "image":$("#txtpro_image").val(),
                    "price":$("#txtpro_price").val(),
                    "albumid":$("#cbxpro_albumidi").val(),
                    "des":tinyMCE.get('txtpro_des').getContent()
                    
                    
            };
            jQuery.ajax({
                    type:"POST", 
                    data:sdata, 
                    dataType:"text", 
                    url:surl, 
                    success: function (data){
                        if(data=="true"){
                            $('div#content_product_detail').block({ 
                                message: 'Đang xử Lý...', 
                                css: { border: '1px solid #a00' } 
                            }); 
                            $("#pro_warningmsg").html("Thành Công. Tải Lại Trang Trong 2 giây.");
                            $("#pro_warningmsg").show();
                            setTimeout(function() {
                                $('div#content_product_detail').unblock();
                                $.unblockUI();
                                loadhtml('Controller/?AUnit=06_product',$('#box-content'));
                            },3000);
                            
                        }else{
                            showerrorbox(null,data);
                            $("#pro_warningmsg").html(data);
                            $("#pro_warningmsg").show();
                        }

                    },
                    error: function (xhr, ajaxOptions, thrownError){
                            $("#pro_warningmsg").html("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
                            $("#pro_warningmsg").show();
                    }
            });

        }
    </script>
